﻿<!DOCTYPE html>
<html >
    <head>
        <title>MySQL demo</title>
    </head>
    <body>
        <div id='log'></div>
    </body>
    <script src="js/jQuery-1.12.4.js"></script>
    <script src="js/org.dbcloud.mysql.Statement.js"></script> 
    <script>

        function log(msg) {
            $("#log").append("<p>" + msg.replace("\n","</p><p>") + "</p>");
        }
        try {
            log("MySQL Test:");
            var stmt = new org.dbcloud.mysql.Statement();            
            if (stmt) {
                var result;
                log("create table:" + JSON.stringify(stmt.execute("create table if not exists temp_user(name char(50) ,id int ) ")));
                var start = new Date().getTime();
                var total = 0;
                for (var j = 0; j < 10; j++) {
                    var sqls = [];
                    for (var i = 0; i < 10; i++) {
                        sqls.push("insert into temp_user(name,id) values('names" + i + "'," + i + ")");
                        total++;
                    }
                    log("" + JSON.stringify(stmt.execute(sqls)) + "");
                }
                log((new Date().getTime() - start) / 1000 + "'s insert " + total + " records!");

                result = stmt.executeQuery("select name ,id  from temp_user", {document: true});
                log("Synchronize the query and return the document structure data, which can be operated directly as a JSON object:");
                log(JSON.stringify(result));
                if (result.rows && result.rows.length > 0)
                {
                    log(result.rows[0].name);
                }
                log("Synchronize query and return two dimensional table structure data:");
                result = stmt.executeQuery("select name ,id  from temp_user");
                log(JSON.stringify(result));
                if (result.error) {
                    log("error:" + result.error.message + "\ncode:" + result.error.code + "\nSQLState:" + result.error.SQLState);
                }
                stmt.executeQuery({done: function (r) {
                        log("Asynchronous query:" + JSON.stringify(r));
                    }, fail: function (e) {
                        log(e.message);
                    }, always: function () {
                        log("Deleting the datasheet and closing the data link");
                        stmt.execute("drop table temp_user")
                        //stmt.close();//If a server shares a link, it should not be closed.
                    }}, "select * from temp_user", {document: false});
            }
        } catch (e) {
            log(e.message);
        }
    </script>

</html>
